package com.zj.ssm.service.impl;

import com.zj.ssm.mapper.NewsTypeMapper;
import com.zj.ssm.pojo.domain.Category;
import com.zj.ssm.pojo.domain.News;
import com.zj.ssm.pojo.domain.query.NewsTypeQuery;
import com.zj.ssm.pojo.domain.vo.CategoryVO;
import com.zj.ssm.result.AjaxResult;
import com.zj.ssm.service.NewsTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * @author wxr
 * @version 1.0
 * @date 2022/2/17 9:08
 */
@Service
public class NewsTypeServiceImpl implements NewsTypeService {
    @Autowired
    private NewsTypeMapper newsTypeMapper;

    @Override
    public AjaxResult queryType(NewsTypeQuery newsTypeQuery) {
        List<Category> news = newsTypeMapper.queryType(newsTypeQuery);
        Long aLong = newsTypeMapper.queryTypeTotal(newsTypeQuery);
        HashMap<Object, Object> objectObjectHashMap = new HashMap<>();
        objectObjectHashMap.put("currentData",news);
        objectObjectHashMap.put("totalCount",aLong);



        return AjaxResult.success(objectObjectHashMap);
    }

    @Override
    public AjaxResult add(CategoryVO categoryVO) {
        Category category = newsTypeMapper.getName(categoryVO);
        if (category == null){
            int i = newsTypeMapper.add(categoryVO);
            return AjaxResult.success("添加成功");
        }
        return AjaxResult.fail("添加失败");
    }

    @Override
    public AjaxResult delete(Integer id) {
        int i = newsTypeMapper.delete(id);
        if (i<0){
            return AjaxResult.fail();
        }
        return AjaxResult.success();
    }

    @Override
    public AjaxResult get(Integer id) {
        CategoryVO c = newsTypeMapper.get(id);
        return AjaxResult.success(c);
    }

    @Override
    public AjaxResult update(CategoryVO categoryVO) {
        int i = newsTypeMapper.update(categoryVO);
        if (i<0){
            return AjaxResult.fail();
        }
        return AjaxResult.success();
    }

    @Override
    public AjaxResult list() {
        List<Category> categories = newsTypeMapper.list();
        return AjaxResult.success(categories);
    }
}
